package com.example.personblog.repository;

import com.example.personblog.entity.Post;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface PostRepository extends JpaRepository<Post, Long> {

    /**
     * 根据用户ID和状态查询文章列表
     */
    List<Post> findByUserIdAndStatusOrderByCreateTimeDesc(Long userId, Integer status);

    /**
     * 分页查询文章列表
     */
    Page<Post> findByStatusOrderByCreateTimeDesc(Integer status, Pageable pageable);

    /**
     * 分页查询用户的文章列表
     */
    Page<Post> findByUserIdAndStatusOrderByCreateTimeDesc(Long userId, Integer status, Pageable pageable);

    /**
     * 增加浏览量
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.views = p.views + 1 WHERE p.id = :id")
    void incrementViews(@Param("id") Long id);

    /**
     * 更新浏览量
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.views = :views WHERE p.id = :postId")
    void updateViews(@Param("postId") Long postId, @Param("views") Long views);

    /**
     * 更新点赞数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.likes = :likeCount WHERE p.id = :postId")
    void updateLikeCount(@Param("postId") Long postId, @Param("likeCount") Long likeCount);

    /**
     * 检查文章是否属于指定用户
     */
    boolean existsByIdAndUserId(Long id, Long userId);

    /**
     * 搜索文章
     */
    @Query("SELECT p FROM Post p WHERE p.status = 1 AND (p.title LIKE %:keyword% OR p.content LIKE %:keyword%)")
    Page<Post> searchPosts(@Param("keyword") String keyword, Pageable pageable);

    /**
     * 增加点赞数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.likes = p.likes + 1 WHERE p.id = :id")
    void incrementLikes(@Param("id") Long id);

    /**
     * 减少点赞数
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.likes = p.likes - 1 WHERE p.id = :id AND p.likes > 0")
    void decrementLikes(@Param("id") Long id);

    /**
     * 批量更新文章状态
     */
    @Modifying
    @Transactional
    @Query("UPDATE Post p SET p.status = :status WHERE p.id IN :ids")
    void updateStatusByIds(@Param("ids") List<Long> ids, @Param("status") Integer status);

    /**
     * 获取用户的文章统计
     */
    @Query("SELECT COUNT(p) FROM Post p WHERE p.userId = :userId AND p.status = :status")
    Long countByUserIdAndStatus(@Param("userId") Long userId, @Param("status") Integer status);

    /**
     * 使用JOIN FETCH查询文章列表
     */
    @Query("SELECT p FROM Post p LEFT JOIN FETCH p.user WHERE p.status = :status ORDER BY p.createTime DESC")
    List<Post> findPostsWithUser(@Param("status") Integer status, Pageable pageable);

    /**
     * 统计文章数量
     */
    long countByStatus(Integer status);
}